package com.xiaoxi.exam.controller;

import cn.xinzhili.xutils.core.ErrorCode;
import cn.xinzhili.xutils.core.http.Response;
import com.xiaoxi.exam.bean.errorcode.UserErrorCode;
import com.xiaoxi.exam.bean.request.AddTelRequest;
import com.xiaoxi.exam.bean.request.UpdateUserInfoRequest;
import com.xiaoxi.exam.bean.request.VerifyCodeRequest;
import com.xiaoxi.exam.bean.response.MobileResponse;
import com.xiaoxi.exam.bean.response.UserInfoResponse;
import com.xiaoxi.exam.service.UserService;
import com.xiaoxi.exam.utils.RegexUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
@Api(description = "用户相关接口")
@RequestMapping("/user")
public class UserController {

  @Autowired
  private UserService userService;

  @ApiOperation(value = "获取openid", notes = "根据code返回用户openId")
  @ApiImplicitParam(name = "code", value = "微信res.code值", required = true, dataType = "String", paramType = "query")
  @PostMapping("/openid")
  public Response getOpenid(@RequestParam(value = "code") String code) {
    return userService.getOpenidByCode(code);
  }


  @ApiOperation(value = "修改用户头像，昵称", notes = "getUserInfo后调用更新数据库用户数据")
  @ApiImplicitParam(name = "request", value = "更新用户实体", required = true, dataType = "UpdateUserInfoRequest")
  @PostMapping("/info")
  public Response updateUserInfo(@RequestBody UpdateUserInfoRequest request) {
    return userService.updateUserInfo(request);
  }

  @ApiOperation(value = "获取用户手机号", notes = "根据openId获取用户手机号")
  @ApiImplicitParam(name = "openid", value = "微信openId", required = true, dataType = "String", paramType = "query")
  @PostMapping("/mobile")
  public Response getMobile(@RequestParam(value = "openid") String openid) {
    MobileResponse response = userService.getMobileByOpenid(openid);
    return Response.instanceSuccess(response);
  }

  @ApiOperation(value = "用户绑定手机（发送验证码）")
  @ApiImplicitParam(name = "request", value = "绑定手机号实体", required = true, dataType = "AddTelRequest")
  @PostMapping("/bind/pre")
  public Response bind(@RequestBody AddTelRequest request) {
    if (RegexUtils.isMobileExact(request.getMobile())) {
      return userService.preBindMobile(request);
    } else {
      return Response.instanceFail(UserErrorCode.MOBILE_ERROR);
    }

  }

  @ApiOperation(value = "验证绑定手机验证码", notes = "绑定手机号")
  @ApiImplicitParam(name = "request", value = "绑定手机号实体（带验证码）", required = true, dataType = "VerifyCodeRequest")
  @PostMapping("/bind")
  public Response bind(@RequestBody VerifyCodeRequest request) {
    if (RegexUtils.isMobileExact(request.getMobile()) || !StringUtils
        .isEmpty(request.getVerifyCode())) {
      return userService.bindMobile(request);
    }
    return Response.instanceFail(ErrorCode.INVALID_PARAMS);
  }


  @ApiOperation(value = "获取用户是否绑定托福账户")
  @ApiImplicitParam(name = "openId", value = "微信openId", required = true, dataType = "String", paramType = "query")
  @GetMapping("/toefl/account")
  public Response getUserInfoByOpenid(@RequestParam("openId") String openId) {
    UserInfoResponse userInfoResponse = userService.getUserInfoResponse(openId);
    return Response.instanceSuccess(userInfoResponse);
  }

}
